package org.gc.common.entity.security;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.gc.common.entity.base.BaseEntity;


/**
 * 权限组，为了方便用户授权而添加此类。
 * <p>
 * 实现的功能：
 * <ul>
 * <li>1、权限组和权限的关系是一对多</li>
 * <li>2、把权限组授权给某一个用户，则用户拥有该组中的所有权限</li>
 * </ul>
 * 
 * @author bl00252
 * 
 */
@Entity
@Table(name = "EL_ROLEGROUP")
public class RoleGroup extends BaseEntity implements Serializable {

	private static final long serialVersionUID = -3528494658062181073L;
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Long id;
	@Column(length = 20, unique = true)
	private String name;
	@Column(length = 64)
	private String description;

	public RoleGroup() {
	}

	@ManyToMany(fetch = FetchType.EAGER)
	@JoinTable(name = "el_rolegroup_role", joinColumns = { @JoinColumn(name = "rolegroup_id") }, inverseJoinColumns = @JoinColumn(name = "role_id"))
	private Set<Role> roles = new HashSet<Role>();
	
	
	@ManyToMany(mappedBy="roleGroups")
    private Set<User> users = new HashSet<User>();

	public void setId(Long id) {
		this.id = id;
	}

	public Long getId() {
		return id;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getDescription() {
		return description;
	}

	public void setRoles(Set<Role> roles) {
		this.roles = roles;
	}

	public Set<Role> getRoles() {
		return roles;
	}

	/**
	 * {@inheritDoc}
	 */
	public boolean equals(Object o) {
		if (this == o) {
			return true;
		}
		if (!(o instanceof RoleGroup)) {
			return false;
		}

		final RoleGroup obj = (RoleGroup) o;

		return !(name != null ? !name.equals(obj.name) : obj.name != null);

	}

	/**
	 * {@inheritDoc}
	 */
	public int hashCode() {
		return (name != null ? name.hashCode() : 0);
	}

	/**
	 * {@inheritDoc}
	 */
	public String toString() {
		return new ToStringBuilder(this, ToStringStyle.SIMPLE_STYLE).append(
				this.name).toString();
	}

	public int compareTo(Object o) {
		return (equals(o) ? 0 : -1);
	}

	public void setUsers(Set<User> users) {
		this.users = users;
	}

	public Set<User> getUsers() {
		return users;
	}

}
